#ifndef __TEXTURE_GLES_H__
#define __TEXTURE_GLES_H__

#pragma once

#include "Texture.h"
#include "RendererGLESCompileConfig.h"

namespace Nezha
{

	class RendererGLES;

	struct PixelBufferGLES
	{
		PixelBufferGLES()
			:mData(NULL)
			,mSizeInBytes(0)
			,mWidth(0)
			,mHeight(0)
		{
		}

		~PixelBufferGLES()
		{
			deallocate();
		}

		void allocate();

		void deallocate();

		void* mData;
		u32 mSizeInBytes;
		u16 mWidth;
		u16 mHeight;
	};

	class TextureGLES : public Texture
	{
	public:
		TextureGLES(HardwareBuffer::Usage usage, const _Image2D& img2d, bool createShadowBuffer,
			bool hasMipmap, bool isCompressed, RendererGLES* r);
		~TextureGLES();

		bool prepare(const u8* data);

		GLuint getGLHandle() const
		{
			return mTexHandle;
		}
	protected:
		// we use shadow buffer always
		virtual void* lockImpl(u32 offset, u32 length, LockOption opt);
		virtual void unlockImpl();
		virtual void syncShadowBuffer();

		void generateMipmaps();

		typedef PArray<PixelBufferGLES> GLESMipmaps;
		GLESMipmaps mMipmaps;

		RendererGLES* mRenderer;
		GLuint mTexHandle;
	};

	class TextureManagerGLES : public TextureManager
	{
	public:
		TextureManagerGLES(RendererGLES* r);

	protected:
		virtual TexturePtr _create2DTexture(const _Image2D& img2d, bool mipmap, HardwareBuffer::Usage usage);

		virtual void releaseTextureImpl(const TexturePtr& tex);

		RendererGLES* mRenderer;
	};

}//end namespace Nezha

#endif //end __TEXTURE_GLES_H__